<?php

class SiteUrlService extends FServiceBase
{    
    /**
    * Get a SiteUrl model given its ID
    * 
    * @param int id SiteUrl ID
    * @return FServiceModel
    */
    public function get($params){
        $model = SiteUrl::model()->findByPk($this->getParam($params, 'id',0));
        if (! $model)
            $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.SiteUrl','Invalid ID.'));
        $this->result->processed('model', $model);
        return $this->result;
    }
    
    public function save($params) {
        /**
        * @var CModel
        */
        $model = $this->getModel($params['SiteUrl'],'SiteUrl');
        $this->result->processed('model', $model);
        
        if (! $model->validate())
            $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.SiteUrl', 'Submitted data is missing or invalid.'));
        elseif ($this->getParam($params, 'validateOnly',0) == TRUE)
            return $this->result;
        elseif (! $model->save())
            $this->result->fail(ERROR_HANDLING_DB, Yii::t('Cms.SiteUrl','Error while saving submitted data into database.'));
        elseif ($model->status && $model->default)
            $this->setDefault(array('id'=>$model->id));
        
        return $this->result;
    }


    public function delete($params) {
        $ids = $this->getParam($params, 'ids', array());
        if ($ids == 0) {
            return $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.SiteUrl','Invalid ID.'));
        }
      
        if (!is_array($ids)) $ids = array($ids);
        foreach($ids as $id) {
            $model = SiteUrl::model()->findByPk($id);
            try {
                $model->delete();
            } catch (CDbException $ex) {
                $this->result->fail(ERROR_HANDLING_DB, $ex->getMessage());
            }
        }
        return $this->result;
    }
    
    public function changeStatus($params) {
        $ids = $this->getParam($params, 'ids', array());
        $value = $this->getParam($params, 'value', false);
        
        if (count($ids) <= 0)
            return $this->result;
        
        $criteria = new CDbCriteria();
        $criteria->addInCondition('id', $ids);
        $models = SiteUrl::model()->findAll($criteria);
        foreach ($models as $model) {
            /**
            * @var Site $model
            */
            $model->status = (boolean) $value;
            if (! $model->save(false, array('status')))
                $this->result->fail(ERROR_HANDLING_DB, Yii::t('CmsModule.Site','Error while saving submitted data into database.'));
        }
        
        return $this->result;
    }
    
    public function setDefault($params)
    {
        $id = $this->getParam($params, 'id', false);
        if ($id) {
            $model = SiteUrl::model()->findByPk($id);
            if (is_object($model) && $model->status) {
                SiteUrl::model()->updateAll(array('default'=>false));
                SiteUrl::model()->updateByPk($id, array('default'=>true));
            }
        }
        return $this->result;
    }
    
    /**
    * delete all site url by site id
    * 
    * @param array $params
    * @return FServiceModel
    */
    public function deleteBySite($params)
    {
        $siteId = $this->getParam($params, 'site_id', 0);
        if ($siteId)
        {
            SiteUrl::model()->deleteAllByAttributes(array('site_id'=>$siteId));
        }
        
        return $this->result;
    }
}